package com.monster.starter.exception.filter;

import com.monster.starter.core.http.response.Response;
import com.monster.starter.exception.common.ExceptionUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 页面出现异常的时候
 * @author kuang
 */
@RestController
@RequestMapping("${server.error.path:${error.path:/error}}")
public class MonsterErrorController implements ErrorController {
    /**
     * 拦截的异常的url
     */
    @Value("${server.error.path:${error.path:/error}}")
    private  String path;
    @Autowired
    private HttpServletResponse response;
    /**
     * 统一处理异常
     */
    @Autowired
    private ExceptionUtils utils;

    /**
     * 获取异常的url
     * @return error url
     */
    public String getErrorPath() {
        return path;
    }

    /**
     * 具体异常的处理
     * @return 返回数据
     */
    @Response(pack = false)
    @RequestMapping
    public String doHandleError() {
        HttpStatus status = HttpStatus.valueOf(response.getStatus());
        return utils.createResultString(String.valueOf(status.value()), status.getReasonPhrase(), null);
    }
}
